package com.ruoyi.web.controller.api;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.api.UUser;
import com.ruoyi.system.service.IUUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.api.UUserAccount;
import com.ruoyi.system.service.IUUserAccountService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户管理Controller
 * 
 * @author ruoyi
 * @date 2022-04-22
 */
@RestController
@RequestMapping("/api/uUserAccount")
public class UUserAccountController extends BaseController
{
    @Autowired
    private IUUserAccountService uUserAccountService;

    @Autowired
    private IUUserService userService;
    /**
     * 查询客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('api:uUserAccount:list')")
    @GetMapping("/list")
    public TableDataInfo list(UUserAccount uUserAccount)
    {
        if (!StringUtils.isEmpty(uUserAccount.getUserPhone())){
            UUser user = new UUser();
            if (uUserAccount.getUserPhone().contains("@")){
                user.setEmail(uUserAccount.getUserPhone());
            }else {
                user.setMobile(uUserAccount.getUserPhone());
            }

            List<UUser> uUsers = userService.selectUUserList(user);
            if (!uUsers.isEmpty()){
                uUserAccount.setUserId(uUsers.get(0).getId());
            }else {
                uUserAccount.setUserId(-1L);
            }
        }
        startPage();
        List<UUserAccount> list = uUserAccountService.selectUUserAccountList(uUserAccount);

        if (!list.isEmpty()){
            for (UUserAccount u:list) {
                UUser user = userService.selectUUserById(u.getUserId());
                if (user != null){
                    if (user.getMobile() != null){
                        u.setUserPhone(user.getMobile());
                    }else {
                        u.setUserPhone(user.getEmail());
                    }
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('api:uUserAccount:export')")
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UUserAccount uUserAccount)
    {
        List<UUserAccount> list = uUserAccountService.selectUUserAccountList(uUserAccount);
        ExcelUtil<UUserAccount> util = new ExcelUtil<UUserAccount>(UUserAccount.class);
        util.exportExcel(response, list, "客户管理数据");
    }

    /**
     * 获取客户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:uUserAccount:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(uUserAccountService.selectUUserAccountById(id));
    }

    /**
     * 新增客户管理
     */
    @PreAuthorize("@ss.hasPermi('api:uUserAccount:add')")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UUserAccount uUserAccount)
    {
        return toAjax(uUserAccountService.insertUUserAccount(uUserAccount));
    }

    /**
     * 修改客户管理
     */
    @PreAuthorize("@ss.hasPermi('api:uUserAccount:edit')")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UUserAccount uUserAccount)
    {
        return toAjax(uUserAccountService.updateUUserAccount(uUserAccount));
    }

    /**
     * 删除客户管理
     */
    @PreAuthorize("@ss.hasPermi('api:uUserAccount:remove')")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(uUserAccountService.deleteUUserAccountByIds(ids));
    }
}
